// import { requestOpenVisualizer } from "../../components/events/globalevents";
// import store from "@/store";
import { baseName, replaceAll } from "../../../utils";

type ENV_VAR = 'INTERPRETER' | 'FILEPATH' | "FILEBASENAME";
export interface FileTreeItemAction {
    icon: 'run' | 'build' | 'visualize',
    label: string,
    action: {
        // Short description to describe what this command is doing.
        // This description will be displayed on the terminals tab.
        shortDesc?: string,
        cmd?: string,
        emitter?: () => void
    }
}
interface FileTreeItemType {
    match: RegExp,
    actions: FileTreeItemAction[]
}



export const FILETREE_ITEMTYPES: FileTreeItemType[] = [
    {
        match: new RegExp("run_simulator\\.py$"),
        actions: [{
            icon: 'visualize',
            label: 'Open Visualizer',
            action: {
                emitter: () => {
                    console.warn("No method registered for emitter")
                    // requestOpenVisualizer()
                }
            }
        }, {
            icon: 'run',
            label: 'Run Simulator',
            action: {
                shortDesc: "Simulator",
                cmd: '$INTERPRETER $FILEPATH'
            }
        }]
    },
    // {
    //     match: new RegExp(".*\\.py$"),
    //     actions: [{
    //         icon: 'run',
    //         label: 'Run this file',
    //         action: {
    //             cmd: '$INTERPRETER $FILEPATH'
    //         }
    //     }]
    // },
]

export const loadItemActions = (fileABSPath: string) => {
    const fileBaseName = baseName(fileABSPath)
    for (const itemType of FILETREE_ITEMTYPES) {
        if (itemType.match.test(fileBaseName)) {
            return itemType.actions
        }
    }
    return []
}


const getEnvVars = (fileABSPath: string): { [key in ENV_VAR]: string } => {
    return {
        INTERPRETER: "",
        FILEPATH: fileABSPath,
        FILEBASENAME: baseName(fileABSPath)
    }
}

export const formatCMD = (fileABSPath: string, cmd: string): string => {
    const envVars = getEnvVars(fileABSPath)
    for (const k in envVars) {
        cmd = replaceAll(cmd, '$' + k, envVars[k as ENV_VAR])
    }
    return cmd
}